package com.danicsoft.daide.token;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Represents a token that contains text. This is a string of two byte words
 * which all start with the TEXT token type (0x4B) and the array consisting of
 * the second bytes makes up and ASCII string.
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class TextToken extends Token {

	/**
	 * The text contained in this token.
	 */
	private String text;

	/**
	 * Construct a new <code>TextToken</code> from the given string.
	 * @param text The text to use as data.
	 */
	public TextToken(String text) {
		super(TokenMap.TEXT);
		this.text = text;
	}

	@Override
	public String getData() {
		return text;
	}

	@Override
	public String toString() {
		return '\'' + text + '\'';
	}

	@Override
	public void output(OutputStream output) throws IOException {
		byte[] strBytes = text.getBytes();
		for (int i = 0; i < strBytes.length; i++) {
			output.write(TokenMap.TEXT);
			output.write(strBytes[i]);
		}

	}

	@Override
	public Token clone() {
		return new TextToken(text);
	}

	@Override
	public int hashCode() {
		return text.hashCode();
	}

}
